﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Authentication;
using System.Data;
using System.Text.RegularExpressions;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace rfins
{
    public class rficode : mysqlconn
    {
        public void rficategory()
        {
            try
            {
                Mailbox rfi_in = new Mailbox("rfi@rhaaia.com");
                FolderId rfi_inbox = new FolderId(WellKnownFolderName.Inbox, rfi_in);
                ItemView iv = new ItemView(int.MaxValue);                
                FindItemsResults<Item> rfis = service.FindItems(rfi_inbox, iv);
                connectwmdata();

                for (int i = 0; i < rfis.Items.Count; i++)
                {
                    Item item = rfis.Items[i];
                    ItemId id = item.Id;
                    EmailMessage email = EmailMessage.Bind(service, id, emailprops);                    
                    string content = email.Subject + email.Body;
                    Match ss = Regex.Match(content, matches[0]);                    
                    Match rfi = Regex.Match(content, matches[1]);
                    Match vendor = Regex.Match(content, matches[2]);
                    Match bid = Regex.Match(content, matches[3]);
                    string strseq = ss.Value;
                    string[] substrseq = Regex.Split(strseq, @"\W");
                    substrseq[1] = substrseq[1].TrimStart(new Char[] { '0' });
                    if (substrseq[1].Length == 0) substrseq[1] = "0";
                    int index = email.Body.Text.LastIndexOf("Question:");
                    string question = email.Body.Text.Substring(index + 10, 50);
                    command.CommandText = "Select str_num, str_seq, city, st from " + database[1] + ".trident where str_num = @varnumber and str_seq = @varsequence and rundate = (select max(rundate) from " + database[1] + ".trident);";
                    command.Parameters.Clear();
                    category = null;
                    DataRow row = MySqlHelper.ExecuteDataRow(conn.ConnectionString, command.CommandText, command.Parameters.AddWithValue("varnumber", substrseq[0]), command.Parameters.AddWithValue("varsequence", substrseq[1]));
                    if (rfi.Length > 0 && vendor.Length == 0 && bid.Length == 0 && row != null) category = row["city"] + " " + row["st"] + " " + substrseq[0] + "-" + substrseq[1] + " - " + rfi.Value;
                    else if (rfi.Length == 0 && vendor.Length > 0 && bid.Length == 0 && row != null) category = row["city"] + " " + row["st"] + " " + substrseq[0] + "-" + substrseq[1] + " - " + vendor.Value;
                    else if (rfi.Length == 0 && vendor.Length == 0 && bid.Length > 0 && row != null) category = row["city"] + " " + row["st"] + " " + substrseq[0] + "-" + substrseq[1] + " - Bid Question: " + question;
                    else if (rfi.Length == 0 && vendor.Length == 00 && bid.Length == 0 && row != null) category = row["city"] + " " + row["st"] + " " + substrseq[0] + "-" + substrseq[1] + " - unknown RFI qualifier";
                    else if (rfi.Length > 0 && vendor.Length == 0 && bid.Length == 0 && row == null) category = rfi.Value;
                    else if (rfi.Length == 0 && vendor.Length > 0 && bid.Length == 0 && row == null) category = vendor.Value;
                    else if (rfi.Length == 0 && vendor.Length == 0 && bid.Length > 0 && row == null) category = "Bid Question: " + question;
                    else category = "Insufficient information to categorize";
                    category = category.Replace(',', ' ');
                    item.Categories.Clear();
                    item.Categories.Add(category);                   
                    item.Update(ConflictResolutionMode.AlwaysOverwrite);                    
                    command.CommandText = "";
                    row = null;
                }
            }
            catch (Exception ex)
            {
                errorhandling.ErrorEmail("rfi code error", ex);
            }
        }
        string category;
        MySqlCommand command = new MySqlCommand();
        ExchangeService service = Service.ConnectToService(UserData.CreateUserData("rfi@rhaaia.com"));
        PropertySet emailprops = new PropertySet(BasePropertySet.FirstClassProperties);
        string[] matches = { @"(\d{3,4}\W\d{1,4})", @"([Rr][Ff][Ii]\W+\d{1,3})", @"([v]\d{3,4}\W\d{1,4}\W\d{1,3})", "bid question" };
    }
}
